package com.project.sianet.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.entidades.*;
import com.project.sianet.util.TypefaceManager;

public class CompetenciaArrayAdapter extends ArrayAdapter<Competencia> {

	private static final String tag = "CompetenciaArrayAdapter";
	private String nomAbrvBim;
    private TextView competencia;
    private TextView nota;
    private View vwNivel1, vwNivel2, vwNivel3, vwNivel4, vwNivel5;
    private LinearLayout lyCompetencia;
    private List<Competencia> competencias = new ArrayList<Competencia>();
    private Context mContext;
    
    public CompetenciaArrayAdapter(Context context, int textViewResourceId,
            List<Competencia> objects, String nomAbrvBim) {
        super(context, textViewResourceId, objects);
        this.competencias = objects;
        this.mContext = context;
        this.nomAbrvBim = nomAbrvBim;
    }
    
    public int getCount() {
        return this.competencias.size();
    }
 
    public Competencia getItem(int index) {
        return this.competencias.get(index);
    }
    
    @Override
    public boolean isEnabled(int position) {

    	return false;

    }
    
	public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            // ROW INFLATION
            Log.d(tag, "Starting XML Row Inflation ... ");
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.competencias_list, parent, false);
            Log.d(tag, "Successfully completed XML Row Inflation!");
        }

        Competencia comp = getItem(position);

        competencia = (TextView) row.findViewById(R.id.comp_nombre);
        nota = (TextView) row.findViewById(R.id.comp_nota);
        vwNivel1 = (View) row.findViewById(R.id.tabNivel1);
        vwNivel2 = (View) row.findViewById(R.id.tabNivel2);
        vwNivel3 = (View) row.findViewById(R.id.tabNivel3);
        vwNivel4 = (View) row.findViewById(R.id.tabNivel4);
        vwNivel5 = (View) row.findViewById(R.id.tabNivel5);
        lyCompetencia = (LinearLayout) row.findViewById(R.id.ly_comp_list);
        
        competencia.setText(comp.getNombre());
        competencia.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.NORMAL);
        nota.setText(Html.fromHtml(comp.getNota()));
        nota.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.NORMAL);
        competencia.setTextColor(mContext.getResources().getColor(android.R.color.black));
		nota.setTextColor(mContext.getResources().getColor(android.R.color.black));
        
        switch (comp.getNivel()) {
        
        	case 1:
        	case 99:
        		lyCompetencia.setBackgroundResource(comp.getNivel() == 99 ? R.drawable.listview_style_nivprom : R.drawable.listview_style_nivel1);
        		vwNivel1.setVisibility(View.VISIBLE);
        		vwNivel1.setBackgroundResource(comp.getNivel() == 99 ? R.drawable.listview_style_nivprom : R.drawable.listview_style_nivel1);
        		vwNivel2.setVisibility(View.GONE);
        		vwNivel3.setVisibility(View.GONE);
        		vwNivel4.setVisibility(View.GONE);
        		vwNivel5.setVisibility(View.GONE);
        		
        		if (comp.getNivel() == 99) {
        			competencia.setTextColor(mContext.getResources().getColor(android.R.color.white));
        			competencia.setText(String.format(mContext.getResources().getString(R.string.lblPromedioBimestral), getNombreBimestre()));
        			nota.setTextColor(mContext.getResources().getColor(android.R.color.white));
        		}
        		
        		break;
        	case 2:
        		lyCompetencia.setBackgroundResource(R.drawable.listview_style_nivel2);
        		vwNivel1.setVisibility(View.VISIBLE);
        		vwNivel1.setBackgroundColor(mContext.getResources().getColor(R.color.nivel1));
        		vwNivel2.setVisibility(View.VISIBLE);
        		vwNivel2.setBackgroundResource(R.drawable.view_nivel2_top_left_style);
        		vwNivel3.setVisibility(View.GONE);
        		vwNivel4.setVisibility(View.GONE);
        		vwNivel5.setVisibility(View.GONE);
        		break;
        	case 3:
        		lyCompetencia.setBackgroundResource(R.drawable.listview_style_nivel3);
        		vwNivel1.setVisibility(View.VISIBLE);
        		vwNivel1.setBackgroundColor(mContext.getResources().getColor(R.color.nivel1));
        		vwNivel2.setVisibility(View.VISIBLE);
        		vwNivel2.setBackgroundResource(R.drawable.view_nivel2_left_style);
        		vwNivel3.setVisibility(View.VISIBLE);
        		vwNivel3.setBackgroundResource(R.drawable.view_nivel3_top_left_style);
        		vwNivel4.setVisibility(View.GONE);
        		vwNivel5.setVisibility(View.GONE);
        		break;
        	case 4:
        		lyCompetencia.setBackgroundResource(R.drawable.listview_style_nivel4);
        		vwNivel1.setVisibility(View.VISIBLE);
        		vwNivel1.setBackgroundColor(mContext.getResources().getColor(R.color.nivel1));
        		vwNivel2.setVisibility(View.VISIBLE);
        		vwNivel2.setBackgroundResource(R.drawable.view_nivel2_left_style);
        		vwNivel3.setVisibility(View.VISIBLE);
        		vwNivel3.setBackgroundResource(R.drawable.view_nivel3_left_style);
        		vwNivel4.setVisibility(View.VISIBLE);
        		vwNivel4.setBackgroundResource(R.drawable.view_nivel4_top_left_style);
        		vwNivel5.setVisibility(View.GONE);
        		break;
        	case 5:
        		lyCompetencia.setBackgroundResource(R.drawable.listview_style_nivel5);
        		vwNivel1.setVisibility(View.VISIBLE);
        		vwNivel1.setBackgroundColor(mContext.getResources().getColor(R.color.nivel1));
        		vwNivel2.setVisibility(View.VISIBLE);
        		vwNivel2.setBackgroundResource(R.drawable.view_nivel2_left_style);
        		vwNivel3.setVisibility(View.VISIBLE);
        		vwNivel3.setBackgroundResource(R.drawable.view_nivel3_left_style);
        		vwNivel4.setVisibility(View.VISIBLE);
        		vwNivel4.setBackgroundResource(R.drawable.view_nivel4_left_style);
        		vwNivel5.setVisibility(View.VISIBLE);
        		vwNivel5.setBackgroundResource(R.drawable.view_nivel5_top_left_style);
        		break;
        
        }
        
        return row;
    }
    
    private String getNombreBimestre() {
    	
    	String n = "";
    	
    	if (nomAbrvBim.substring(0,1).toLowerCase(Locale.getDefault()).equals("b"))
    		n = mContext.getResources().getString(R.string.lblBimestre);
    	else if (nomAbrvBim.substring(0,1).toLowerCase(Locale.getDefault()).equals("t"))
    		n = mContext.getResources().getString(R.string.lblTrimestre);
    	else if (nomAbrvBim.substring(0,1).toLowerCase(Locale.getDefault()).equals("s"))
    		n = mContext.getResources().getString(R.string.lblSemestre);
    	else
    		n = mContext.getResources().getString(R.string.lblAbrvAnual);
    	
    	return n.toUpperCase(Locale.getDefault());
    	
    }
	
}
